package com.geologyoa.persist;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.geologyoa.commons.JdbcUtil;
import com.geologyoa.domain.Roles;

public class RolesDaoImpl implements RolesDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	public Boolean AddRoles(Roles roles) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		  sql = "insert into roles values(oa_seq.nextval,?)";
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, roles.getRolename());
//				ps.setString(2,roles.getFunction());
//				ps.setString(3, roles.getRolefunction());
				int number=ps.executeUpdate();
				if(number>0)
				{
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}
	public List<Roles> SearchAllRoles() {
		Roles roles=null;
		List<Roles> roleList=new ArrayList<Roles>();
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "select * from roles";
			try {
				ps = conn.prepareStatement(sql);
				rs=ps.executeQuery();
				while(rs.next())
				{
					roles=new Roles();
					roles.setRoleid(Integer.parseInt(rs.getString("roleid")));
				    roles.setRolename(rs.getString("rolename"));
//				    roles.setFunction(rs.getString("function"));
//				    roles.setRolefunction(rs.getString("rolefunction"));
					roleList.add(roles);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return roleList;
	}
	public Boolean CheckRoles(String RoleName) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "select * from roles where rolename=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, RoleName);
				int number=ps.executeUpdate();
				if(number>0)
				{
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}
	public Boolean CheckRolesExceptId(String RoleName,String roleId) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "select * from roles where rolename=? and ROLEID !="+roleId;
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, RoleName);
				int number=ps.executeUpdate();
				if(number>0)
				{
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}
	public List<Roles> SearchRoles(String rolename) {
		Roles roles=null;
		List<Roles> roleList=new ArrayList<Roles>();
		   conn=JdbcUtil.getConnection();
		   StringBuffer sql = new StringBuffer();	
		   sql.append("select * from roles where 1=1");
		      if(!rolename.equals("")&&rolename!=null&&!rolename.equals("null"))
		      {
		    	  sql.append(" and rolename like'%"+rolename+"%'");  
		      }
			try {
				ps = conn.prepareStatement(sql.toString());
				rs=ps.executeQuery();
				while(rs.next())
				{
					roles=new Roles();
					roles.setRoleid(Integer.parseInt(rs.getString("roleid")));
				    roles.setRolename(rs.getString("rolename"));
					roleList.add(roles);
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return roleList;
	}
	public Boolean DelRoleInfo(String roleid) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "delete from roles  where roleid=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, roleid);
				int number=ps.executeUpdate();
				if(number>0)
				{
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}
	public Boolean UpdateRoleInfo(Roles roles) {
		Boolean flag =false;
		   conn=JdbcUtil.getConnection();
		   String sql = "";
		   sql = "update roles set rolename=? where roleid=?";
			try {
				ps = conn.prepareStatement(sql);
				ps.setString(1, roles.getRolename());
				ps.setInt(2, roles.getRoleid());
			
				int number=ps.executeUpdate();
				if(number>0)
				{
					flag=true;
				}
				
			} catch (SQLException e) {
				e.printStackTrace();
			} finally
			{
				JdbcUtil.closeAll(ps,conn);
			}
			return flag;
	}

}
